<#import '/ftlLibrary/properteisVariable.ftl' as pv/>
<style>
	.classesLessonsDetailList .modal-dialog {
		width: 82% !important;
	}
</style>
<div class="row" style="margin: 0 1px;">
	<input type="hidden"  name="memberPackageId" class="memberPackageId" value="${memberPackageId}"/>
	<p class="text-right" style="padding: 20px 0;margin: 0;">会员${stuName}，可选课课时数：<span class="dataExpendClassHour">${surplusClassHour}</span>课时</p>
	<#list listMap as data>
		<div class="panel-group" id="accordion">
			<div class="panel panel-info">
				<div class="panel-heading">
					<h4 class="panel-title" data-toggle="collapse" data-parent="#accordion"
						href="#collapse${data_index+1}" style="cursor: pointer;">
						<a>${data.courseName} / ${data.courseName} /  ${data.courseName}</a>
						<a class="float-right">收起</a>
					</h4>
				</div>
				<div id="collapse${data_index+1}" class="panel-collapse collapse in">
					<div class="panel-body">
						<p class="text-center">课程日期：${data.startDate}-${data.endDate} ${data.startTime}-${data.endTime}，${data.weekDay} 课程节数：${data.expendClassHour}节 单节消耗课时数：${data.pitchNumber}</p>
						<form class="bs-example bs-example-form" role="form">
							<div class="row" id="checkbox${data_index+1}">
								<div class="btn-toolbar" role="toolbar" style="margin: 10px;">
									<div class="widget flat radius-bordered" style="margin-top: 0;box-shadow:0px 0px 10px rgba(122,161,184,0.3);">
										<div class="widget-body no-padding">
											<div class="table-scrollable checkboxAllTable" style="border: none;" id="tableScrollable">
												<!-- 数据表列信息列表 -->
												<table class="table table-hover dataTable newtable">
													<thead>
													<tr>
														<th title="选择" class="checkboxColumnTh">
															<div class="checkbox">
																<label>
																	<input type="checkbox" class="chkAll chkAllCount">
																	<span class="text"></span>
																</label>
															</div>
														</th>
														<th title="课程周次">课程周次</th>
														<th title="上课日期">上课日期</th>
														<th title="上课时间">上课时间</th>
														<th title="周几上课">周几上课</th>
														<th title="授课老师">授课老师</th>
														<th title="上课教室">上课教室</th>
														<th title="耗课时数">耗课时数</th>
													</tr>
													</thead>
													<tbody>
													<#if page_bean.list?size==0>
														<tr>
															<td align="center">无数据</td>
														</tr>
													</#if>
													<#list data["lessonsList"] as cl>
														<tr>
															<td class="checkboxColumnTd">
																<div class="checkbox">
																	<label>
																		<input type="checkbox" class="chk chkCount" name="clId" value="${cl.id}">
																		<span class="text"></span>
																	</label>
																</div>
															</td>
															<td>${cl.classIndexTime}</td>
															<td>${cl.classTime}</td>
															<td>${cl.startTime}-${cl.endTime}</td>
															<td><@dictionary groupCode="weekDay" dictionaryCode="${data.weekDay}";weekDay>${weekDay.value }</@dictionary></td>
															<td>${cl.realName}</td>
															<td>${cl.roomName}</td>
															<td class="expendClassHour">${data.expendClassHour}</td>
														</tr>
													</#list>
													</tbody>
												</table>
											</div>
											${pagingBar}
										</div>
									</div>
								</div>
							</div>
						</form>
					</div>
				</div>
			</div>
		</div>
	</#list>

	<div class="col-sm-12 text-align-center">
		<button style="width: 80px;margin-right: 20px" type="button" class="btn btn-blue showDialogAttendClassLessonsBtn" data-dismiss="modal" serializeNum="${serializeNum}" classId="${classId}"
				targetdataId="${targetDataId}" onclick="showDialogAttendChangeClass(this)">确定
		</button>
		<input style="width: 80px;" type="button" class="btn btn-blue" data-dismiss="modal" value="取消" />
	</div>
</div>



<script src="assets/js/validation/bootstrapValidator_init.js"></script>
<script>

    var classHourVal = 0;
    var dataExpendClassHour = $(".dataExpendClassHour").html();
    // 全选计算课时数
    $(".chkAllCount").click(function () {
        var checked = $(this).prop("checked");
        //		算出全部课时 当点击全选算出全部选中的课时总数
        var newClassHourVal = 0;
        $(".checkboxAllTable").each(function (tableIndex,tableItem) {
            var sum= 0;
            var expendClassHour;
            $(tableItem).find(".chkCount").each(function () {
                expendClassHour = $(this).parent().parent().parent().siblings('.expendClassHour').html();
                var val = $(this).prop("checked");
                if(val){
                    sum++;
                }
            })
            newClassHourVal = newClassHourVal + sum * Number(expendClassHour);
        })

        //		算出当前课时数
        var checkIndex = [];
        var thisExpendClassHour;
        var checkboxAllTable = $(this).parents(".checkboxAllTable");
        var size = checkboxAllTable.find(".chkCount").size();
        checkboxAllTable.find(".chkCount").each(function (i,item) {
            thisExpendClassHour = $(item).parent().parent().parent().siblings('.expendClassHour').html();
            var val = $(item).prop("checked");
            if(val){
                checkIndex.push(i);
            }
        })
        var thisCountVal = checkIndex.length * Number(thisExpendClassHour);
        var thisAllCountVal = size *Number(thisExpendClassHour);
        var sumVal = thisAllCountVal - thisCountVal;
        //		根据选择 取消 算出差值 赋值classHourVal变量
        if(checked){
            if(newClassHourVal + sumVal<= Number(dataExpendClassHour)){
                classHourVal  = newClassHourVal + sumVal;
                if(classHourVal == Number(dataExpendClassHour)){
                    $(".showDialogAttendClassLessonsBtn").prop("disabled",false);
				}else{
                    $(".showDialogAttendClassLessonsBtn").prop("disabled",true);
				}

            }else{
                logger.error('可选课时数不足，请购买或激活其他课时包');
                $(this).prop("checked", false);
                checkboxAllTable.find(".chkCount").each(function (i,item) {
                    if(checkIndex.indexOf(i)==-1){
                        $(item).prop("checked", false);
                    }
                })
                return false;
            }

        }else{
            classHourVal  = newClassHourVal - thisCountVal;
            if(classHourVal == Number(dataExpendClassHour)){
                $(".showDialogAttendClassLessonsBtn").prop("disabled",false);
            }else{
                $(".showDialogAttendClassLessonsBtn").prop("disabled",true);
			}
        }

    })

    $(".chkCount").click(function () {
        if ($(this).is(':checked')) {
            var expendClassHour = $(this).parent().parent().parent().siblings('.expendClassHour').html();
            if(classHourVal + Number(expendClassHour) <= dataExpendClassHour){
                classHourVal += Number(expendClassHour);
                $(".showDialogAttendClassLessonsBtn").prop("disabled",false);
            } else {
                logger.error('可选课时数不足，请购买或激活其他课时包');
                $(this).prop("checked", false);
                return false;
            }
        } else {
            var expendClassHour = $(this).parent().parent().parent().siblings('.expendClassHour').html();
            classHourVal -= Number(expendClassHour);
        }
        if(classHourVal == Number(dataExpendClassHour)){
            $(".showDialogAttendClassLessonsBtn").prop("disabled",false);
        }else{
            $(".showDialogAttendClassLessonsBtn").prop("disabled",true);
        }
    });


    $(function () {
        $('#collapseOne').collapse('toggle');
        $(".showDialogAttendClassLessonsBtn").prop("disabled",true);
    });


    for (var i=1;i<=$(".panel-group").length;i++) {
        $('#collapse'+i).on('show.bs.collapse', function () {
            $(this).siblings(".panel-heading").children(".panel-title").children(".float-right").html("收起")
        })
        $('#collapse'+i).on('hide.bs.collapse', function () {
            $(this).siblings(".panel-heading").children(".panel-title").children(".float-right").html("展开")
        })

		checkboxChange(i);

	}

	function checkboxChange(i) {
        $(".checkboxs"+i).change(function () {
            if ($(this).is(':checked')) {
                $("#checkbox"+i+" input").prop("checked", true);
            } else {
                $("#checkbox"+i+" input").prop("checked", false);
            }
        })
	}



    $(".selectpicker_${serializeNum}").select2();

    function showDialogAttendChangeClass(that) {
        var memberPackageId = $(".memberPackageId").val();
        var targetdataId = $(that).attr("targetdataId");
        var classId = $(that).attr("classId");
        var allCheckVlaues = new Array();
        $("[name='clId']:checked").map(function (index, chk) {
            var value = chk.value;
            /*  var values = new Array();
              $("[parentCheckId='clId_" + value + "']:checked").map(function (index, chek1) {
                  values.push(chek1.value);
              });
              allCheckVlaues.push(value + ":" + values.join(","));*/
            allCheckVlaues.push(value);
        });
        var allCheckVlauesStr = allCheckVlaues.join(",");
        ajaxRequest("tssMemberChangeClassRecordController/saveOrUpdate.do", {
            targetDataId: targetdataId,
			classId:classId,
            lessonIds:allCheckVlauesStr,
            memberPackageId: memberPackageId
        }, function (result) {
            if(result.resCode == 200){
                bootbox.hideAll();
                logger.info(result.resMsg, true);
                toView('tssMemberPackageController/tssMemberPackageList.do?type=1',true,'POST',{pageSize:10})
            } else {
                logger.error(result.resMsg);
            }

        }, function () {
            ajaxError("加载页面失败!")
        }, "post", "json")
    }


    /**
     * 获取指定复选框选中的值字符串
     * @param inputName 复选框name属性值,默认是id
     * @param separator 分隔符,默认为逗号
     * @returns {String} 复选框选中的值字符串
     */
    function getCheckedInputValuesClId(inputName, separator) {
        if (!inputName) {
            inputName = "clId";
        }
        if (!separator) {
            separator = ",";
        }
        var inputs = $("[name='" + inputName + "']:checked");
        var count = inputs.size();
        var values = "";
        for (var i = 0; i < count; i++) {
            values += inputs.eq(i).val();
            if (i != count - 1) {
                values += ",";
            }
        }
        return values;
    }
</script>